const express = require('express');
//引入mysql
const pool = require('../pool.js');
//创建路由
const router = express.Router();


//根据硬件名称获取商品列表

router.get('/list',(req,res)=>{
    var list  = req.query;

    if(!list.page){
        list.page = 1;
    }
    if(!list.pagesize){
        list.pagesize = 10;
    }
    var page = (list.page - 1) * list.pagesize;
    var pagesize = parseInt(list.pagesize);
    //定义sql语句 按照硬件名称查询商品并按照销量排序分页显示
    const sql = ' select s.id,s.sname,s.price,s.imgpath,sp.svalue,sp.socre from shop s join shopexpand sp on s.id = sp.cid  where sp.categoryName=? order by  sp.xredissale limit ?,?';
    
    pool.query(sql,[list.categoryName,page,pagesize],(err,r)=>{
        if(err){
            return res.cc(err);
        }
        console.log(r.length);
        if(r.length  == 0){
            return res.cc({code:200,msg:'查询失败'});
        }

        pool.query('select count(*) count from shopexpand where categoryName=?',[list.categoryName],(err,db)=>{
            console.log(db[0].count);
            if(err){
                return res.cc('查询失败');
            }
            res.send({code:200,msg:'操作数据成功',data:r,page:list.page,pagesize:list.pagesize,MaxpageSize:parseInt(db[0].count / page)});
        });

        
    });

});

// router.get('/like',(req,res)=>{
//     pool.query('select * from shop where sname like %?%',[req.query.type],(err,r)=>{
//         if(err){
//             return res.cc(err);
//         }

//         if(r.length == 0){
//             return res.cc('查询失败');
//         }

//         res.send({code:200,msg:'操作数据成功',data:r});
//     })
// });

//根据商品Id获取商品信息

router.get('/details',(req,res)=>{
    const info = req.query;
    pool.query('select s.*,sp.socre,sp.svalue from shop s join shopexpand sp on s.id = sp.cid where s.id=?',[info.id],(err,r)=>{
        if(err){
            return res.cc(err);
        }

        if(r.length == 0){
            return  res.cc('查询失败');
        }

        res.send({code:200,msg:'操作数据成功',data:r});
    });
});











module.exports = router;